<?php

class Application_Model_DbTable_Sach extends Zend_Db_Table_Abstract
{

    protected $_name = 'SACH';

    public function getLastInsertId(){
        $select = $this->select();
        $select->from($this->_name,array(new Zend_Db_Expr('max(MaSach) as MaSach')));
        $rows = $this->fetchRow($select);
        return ($rows->MaSach+1);
    }
    
    
    
    
    public function getDataTable($args = array() ){
        $select = $this->select();
        $select->from(array('sach'=>$this->_name), array("MaSach","TenSach","TacGia","SoLuongTon","DonGiaNhap"))
               ->join(array('tl'=>"THELOAISACH"),"sach.MaTheLoai=tl.MaTheLoaiSach",array('MaTheLoai'=>"tl.MaTheLoaiSach","TenTheLoai"=>'tl.TenTheLoai'))
               ->order("MaSach asc")
               ->setIntegrityCheck(false);
               
        if(is_string($args)){
            $term = array();
            parse_str($args,$term );
            $args = $term;
        }
               
        if(isset($args['MaSach'])&&$args['MaSach']!=""){
            $select->where("MaSach=?",$args['MaSach']);
        }
        
        if(isset($args['MaTheLoai'])&&$args['MaTheLoai']!=""){
            $select->where("MaTheLoai=?",$args['MaTheLoai']);
        }
        
        if(isset($args['TenSach'])&&$args['TenSach']!=""){
            $select->where("TenSach LIKE ?","%{$args['TenSach']}%");
        }
        
        if(isset($args['TacGia'])&&$args['TacGia']!=""){
            $select->where("TacGia LIKE ?","%{$args['TacGia']}%");
        }
        
        if(isset($args['slTonMin'])&&$args['slTonMin']!=""){
            $select->where("SoLuongTon >= ?",$args['slTonMin']);
        }
        
        if(isset($args['slTonMax'])&&$args['slTonMax']!=""){
            $select->where("SoLuongTon <= ?",$args['slTonMax']);
        }
        
        if(isset($args['slGiaMin'])&&$args['slGiaMin']!=""){
            $select->where("DonGiaNhap >= ?",$args['slGiaMin']);
        }
        
        if(isset($args['slGiaMax'])&&$args['slGiaMax']!=""){
            $select->where("DonGiaNhap <= ?",$args['slGiaMax']);
        }
        
        return $select;
    }
    
    public function deleteBook($id){
        $row = $this->find($id)->current();
        if($row){
            return $row->delete();
        }
        return false;
    }
    
    public function insertNewBook($args){
        if(is_string($args)){
            $term = array();
            parse_str($args,$term );
            $args = $term;
        }
        $row = $this->createRow($args);
        $row->save();
        return $this->getLastInsertId();
    }
    
    public function editBook($args){
        if(is_string($args)){
            $term = array();
            parse_str($args,$term );
            $args = $term;
        }
        $row = $this->find($args['MaSach'])->current();
        foreach($args as $key=>$value){
            $row->{$key} = $value;
        }
        return $row->save();        
    }
    
    public function getBook($id){
        return $this->find($id)->current()->toArray();
    }
}

